<?php  

import('miho.config.configurator.resource.AbstractConfiguratorResource');
import('miho.controller.FrontController');

/**
* 
*/
class FrontControllerResource extends AbstractConfiguratorResource
{
	
	protected $_frontController;
	protected $_configurator;
	
	public function __construct()
	{
		$this->_frontController = FrontController::getInstance();
	}
	
	public function init()
	{
		$this->_initRouter()
			 	 ->_initRequest();
	}
	
	protected function _initRouter(){
		$routerClass = 'miho.router.pattern.PatternRouter';
		
		if($this->getOption('routerClass')){
			$routerClass = $this->getOption('routerClass');
		}
		
		import($routerClass);		
    $router = ClassLoader::getInstance()->guessClassName($routerClass);
    $this->_frontController->setRouter(new $router());
    if($this->getOption('routes')){
    	$this->_frontController->getRouter()->configure($this->getOption('routes'));
    }
    
    return $this;
	}
	
	protected function _initRequest(){
		$requestClass = 'miho.request.http.HttpRequest';
		
		if($this->getOption('requestClass')){
			$requestClass = $this->getOption('requestClass');
		}
		
		import($requestClass);		
    $request = ClassLoader::getInstance()->guessClassName($requestClass);
    $this->_frontController->setRequest(new $request());
    
    return $this;	
	}
	
	public function run(){
		$this->_frontController->dispatch();
	}
}

